package com.db4o.odbgen.plugins.java.maingenerator.typedxmlschema;

import java.util.ArrayList;
import java.util.List;

import com.db4o.internal.odbgen.StringUtils;
import com.db4o.odbgen.plugins.xmlschema.Element;

/**
 * 
 * Represents an enumeration constant.
 * @author liviug
 */
public class TSEnumConstant implements XmlElement{
    
    private String _name;
    private List<TSEnumValue> _values;
    private Element _element;

    /**
     * Constructs an enum constant given it's fields.
     * @param element The corresponding element inside XmlSchema.
     * @param name The name of the constant.
     * @param values The values associated with this constant.
     * This collection must be sorted by the order of the parameters.
     */
    public TSEnumConstant(
            Element element, 
            String name,
            List<TSEnumValue> values
            ){
        this._element = element;
        this._name = name;
        this._values = values;
    }
    
    
    /**
     * See {@link XmlElement} for details.
     */
    @Override
    public Element getElement() {
        return this._element;
    }
    
    public String getName(){
        return this._name;
    }
    
    /**
     * Returns the list of values for this constant
     * This is sorted by the order of the parameters.
     * If the collection is modified, the order must be preserved according
     * to the order of the parameters.
     * This can never be null. 
     */
    public List<TSEnumValue> getValues(){
        if(this._values==null){
            this._values = new ArrayList<TSEnumValue>();
        }
        return this._values;
    }


    /**
     * Returns a string representing this object.
     * This is used for debug purposes.
     * @param tabs Number of tabs to indent.
     */
    String toString(int tabs) {
        StringBuilder res = new StringBuilder();
        
        res.append(String.format("%sconstant", StringUtils.tabs(tabs)));
        res.append(StringUtils.newLine());

        res.append(String.format("%sname=%s",
                StringUtils.tabs(tabs+1),
                this.getName()
                ));
        res.append(StringUtils.newLine());

        for(TSEnumValue val : this.getValues()){
            res.append(val.toString(tabs+1));
            res.append(StringUtils.newLine());
        }
        
        return res.toString();
    }
    
}
